import { Select, SelectProps } from 'antd';
import { useEffect, useState } from 'react';

import DictionarySubDataService from '@/api/services/stars/dictionarySubDataService';

export interface DictSelectProps<ValueType = any>
  extends Omit<SelectProps<ValueType | ValueType[]>, 'options' | 'children'> {
  dictKey: string;
}
export function DictSelect({ dictKey, ...props }: DictSelectProps) {
  const [dictData, setDictData] = useState<{ label: string; value: string }[]>([]);

  useEffect(() => {
    DictionarySubDataService.listByKey(dictKey, {}).then((res) => {
      setDictData(
        res.map((dictItem) => {
          return {
            label: dictItem.value,
            value: dictItem.id,
          };
        }),
      );
    });
  }, [dictKey]);

  return <Select options={dictData} {...props} />;
}
